/**
 * 二分查找
 * @param {character[]} letters
 * @param {character} target
 * @return {character}
 */
export var nextGreatestLetter = function (letters, target) {
  let left = 0,
    right = letters.length - 1
  if (target >= letters[letters.length - 1]) {
    return letters[0]
  }
  while (left < right) {
    const mid = (left + right) >> 1
    if (letters[mid] > target) {
      right = mid
    } else {
      left = mid + 1
    }
  }
  return letters[left]
}

/**
 * @param {character[]} letters
 * @param {character} target
 * @return {character}
 */
export var nextGreatestLetter2 = function (letters, target) {
  return (letters.find(letter => letter > target) || [letters[0]])[0]
}

/**
 * @param {character[]} letters
 * @param {character} target
 * @return {character}
 */
export var nextGreatestLetter3 = function (letters, target) {
  if (letters[letters.length - 1].charCodeAt() < target.charCodeAt()) {
    return letters[0]
  }
  for (const letter of letters) {
    if (letter.charCodeAt() > target.charCodeAt()) {
      return letter
    }
  }
}
